﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Antlr.Runtime;
using wne.Context;
using wne.core;
using wne.core.Interface;
using wne.models;
using wne.models.Filter;

namespace wne.Controllers
{
    public class DocumentController : Controller
    {
        private readonly DocumentFacade facade;
        private readonly AdminFacade facade_admin;
        private readonly AccountFacade facade_account;
        public DocumentController()
        {
            facade = new DocumentFacadeSql();
            facade_admin = new AdminFacadeSql();
            facade_account= new AccountFacadeSql();
        }

        #region Docs

        public ActionResult EditDoc()
        {
            ViewBag.orderNum = "1";
            ViewBag.searchString = "";
            var fil = new RouteFilter_l();
            fil.SearchString = "";
            List<RouteModel> routes = facade_admin.GetFilteredRoutes(fil);
            ViewBag.routes = routes;
            return View(routes);
        }


        [HttpPost]
        public ActionResult EditDoc(String orderNum, String searchString)
        {
            ViewBag.orderNum = orderNum;
            ViewBag.searchString = searchString;
            var filter = new RouteFilter_l();
            filter.SearchString = searchString;
            filter.IsOrderDesc = ("1".Equals(orderNum));
            filter.OrderEnum = (RouteOrderEnum) 1;
            List<RouteModel> m = facade_admin.GetFilteredRoutes(filter);
            return View(m);
        }


        public ActionResult NewDoc(int type)
        {
            var model = new DocModel();
            model.doc = new Doc();
            using (var context = new tpksEntities())
                model.doc.Owner_ID =
                    model.doc.Current_User_ID =
                        context.User.Where(x => x.Login.Equals(User.Identity.Name)).First().User_ID;
            model.doc.Way_ID = type;
            model.doc.Doc_Name = "";
            ViewBag.UserName = facade_admin.GetUserById(model.doc.Owner_ID).UserName;
            model = facade.StartDoc(model);
            return View(model);
        }

        [HttpPost]
        public ActionResult NewDoc(DocModel m, int Doc_Type, int Way_ID, string Doc_Name, List<string> fields, string optionsRadios)
        {
            m = new DocModel();
            m.doc = new Doc();
            m.doc.Way_ID = Way_ID;
            m.doc.Doc_Name = Doc_Name;
            m.doc.Doc_Type = Doc_Type;
            using (var context = new tpksEntities())
                m.doc.Owner_ID =
                    m.doc.Current_User_ID =
                        context.User.Where(x => x.Login.Equals(User.Identity.Name)).First().User_ID;
            ViewBag.UserName = facade_admin.GetUserById(m.doc.Owner_ID).UserName;

            var i = 0;
            m = facade.StartDoc(m);
            foreach (var f in m.fields)
            {
                if (f.type.Visible_Flag == "1" && f.type.Editable_Falg == "1" && f.type.Field_Type != 7 &&
                    f.type.Field_Type != 10)
                {
                    f.Field.Value = fields[i];
                    i++;
                }
            }
            facade.CreateDoc2(m);
            if (optionsRadios == "option2")
            {
                m.accept = true;
                facade.DocGo(m);
            }
            return Redirect("EditDoc");
        }


        public ActionResult DocumentView(int? docId)
        {
            var model = new DocModel();
            if (docId != null) model = facade.GetDocById((int) docId, 0, 0);
            //.now = new DocModel();
            //model.now.doc = new Doc();
            //{doc = {Doc_ID = 123,Doc_Name = "qweeweeq"}}
            ViewBag.Owner_Name = facade_admin.GetUserById(model.doc.Owner_ID).UserName;


            return View(model);
        }

        [HttpPost]
        public ActionResult DocumentView(DocModel m, int Doc_ID, List<string> fields, string optionsRadios)
        {
            var model = new DocModel();
            model = facade.GetDocById(Doc_ID, 0, 0);
            int i = 0;
            foreach (var f in model.fields)
            {
                if (f.type.Visible_Flag == "1" && f.type.Editable_Falg == "1" && f.type.Field_Type != 7 &&
                    f.type.Field_Type != 10)
                {
                    f.Field.Value = fields[i];
                    i++;
                }
            }
            
            
            ViewBag.Owner_Name = facade_admin.GetUserById(model.doc.Owner_ID).UserName;

            if (optionsRadios == "option1")
            {
                model.decline = true;
                facade.DocGo(model);
                return Redirect("EditDoc");
            }
            if (optionsRadios == "option2")
            {
                model.accept = true;
                facade.DocGo(model);
                return Redirect("EditDoc");
            }
            if (optionsRadios == "option3")
            {
                facade.UpdateDoc(model);
                return View(model);
            }

            return Redirect("EditDoc");
        }


        public ActionResult DocsDoneList()
        {
            var model = new DocModel();

            DocFilter filter = new DocFilter();
            filter.SearchString = "";
            filter.IsOrderDesc = true;
            filter.OrderEnum = (DocOrderEnum)1;
            filter.IsOrderDesc = false;
            filter.end = null;
            filter.start = null;
            var id = facade_account.CurrentUserId(User.Identity.Name);
            return View(facade.GetFilteredDocsDone(filter, id)); // тут проставить айди текущего пользователя!!!
        }

        [HttpPost]
        public ActionResult DocsDoneList(String searchString)
        {
            DocFilter filter = new DocFilter();
            filter.SearchString = "";
            filter.IsOrderDesc = true;
            filter.OrderEnum = (DocOrderEnum)1;
            var id = facade_account.CurrentUserId(User.Identity.Name);
            return View(facade.GetFilteredDocsDone(filter, id)); // тут проставить айди текущего пользователя!!!
        }

        //InComing!
          public ActionResult InComeDoc()
          {
              var model = new DocModel();

              DocFilter filter = new DocFilter();
              filter.SearchString = "";
              filter.IsOrderDesc = true;
              filter.OrderEnum = (DocOrderEnum)1;
              filter.IsOrderDesc = false;
              filter.end = null;
              filter.start = null;
              var id = facade_account.CurrentUserId(User.Identity.Name);
              return View(facade.GetFilteredDocsAvailable(filter, id)); // тут проставить айди текущего пользователя!!!(В процессе)
          }

          [HttpPost]
          public ActionResult InComeDoc(String searchString)
          {
              DocFilter filter = new DocFilter();
              filter.SearchString = "";
              filter.IsOrderDesc = true;
              filter.OrderEnum = (DocOrderEnum)1;
              var id = facade_account.CurrentUserId(User.Identity.Name);
              return View(facade.GetFilteredDocsAvailable(filter, id)); // тут проставить айди текущего пользователя!!!(В процессе)
          }


          //Editable!
          public ActionResult EditableDocsList()
          {
              var model = new DocModel();

              DocFilter filter = new DocFilter();
              filter.SearchString = "";
              filter.IsOrderDesc = true;
              filter.OrderEnum = (DocOrderEnum)1;
              filter.IsOrderDesc = false;
              filter.end = null;
              filter.start = null;
              var id = facade_account.CurrentUserId(User.Identity.Name);
              return View(facade.GetFilteredDocs(filter, id)); // тут проставить айди текущего пользователя!!!(В процессе)
          }

          [HttpPost]
          public ActionResult EditableDocsList(String searchString)
          {
              DocFilter filter = new DocFilter();
              filter.SearchString = "";
              filter.IsOrderDesc = true;
              filter.OrderEnum = (DocOrderEnum)1;
              var id = facade_account.CurrentUserId(User.Identity.Name);
              
              return View(facade.GetFilteredDocs(filter, id)); // тут проставить айди текущего пользователя!!!(В процессе)
          }

       

        #endregion
    }
}